﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InvasionWars
{
    public class Zone
    {
        public int ZoneID
        {
            get { return zoneID;}
        }
        private int zoneID;
        public Armies Controller { get; set; }
        public Card HumanSlot{ get;  set; }
        public Card AlienSlot{ get; set; }


        public Zone(int id)
        {
            zoneID = id;
            Controller = Armies.NULL;
        }

        public void Battle()
        {
            if(Controller == Armies.NULL)
            {
                //play out battle
                Card winner;
                if (HumanSlot.Speed > AlienSlot.Speed)
                {
                    winner = FindWinner(HumanSlot, AlienSlot);
                }
                else
                {
                    winner = FindWinner(AlienSlot, HumanSlot);
                }

                Controller = winner.Army;

                RemoveLoserCard();
            }

        }

        public Card FindWinner(Card faster, Card slower)
        {
            if (faster.Attack > slower.Defense)
                return faster;
            else
            {
                int slowDefense = slower.Defense;
                int fastDefense = faster.Defense;
                while (slowDefense > 0 && fastDefense > 0)
                {
                    slowDefense -= (faster.Attack - (faster.Speed / 4));
                    fastDefense -= (slower.Attack - (slower.Speed / 4));
                }

                if (fastDefense > 0)
                    return faster;
                else
                    return slower;
            }
            return faster;
        }

        public void RemoveLoserCard()
        {
            if (HumanSlot.Army != Controller)
            {
                HumanSlot = AlienSlot;
                AlienSlot = null;
            }
            else
            {
                AlienSlot = HumanSlot;
                HumanSlot = null;
            }
        }
    }

}
